package com.cman777.springc.mybatisgenerator.generator;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.cman777.springc.mybatisgenerator.properties.MybatisGeneratorProperties;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenzhicong
 * @time 2020/3/16 18:53
 * @description
 */
public class JavaFileGenerator {

    /**
      * 该方法由项目中的test生成
      */
    public static void generateJavaFile(MybatisGeneratorProperties properties){
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor(properties.getAuthor());
        gc.setOpen(false);
        gc.setFileOverride(false);
        gc.setServiceName("%sService");
        // gc.setSwagger2(true); 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(properties.getDataSourceUrl());
        //dsc.setSchemaName("public");
        dsc.setDriverName(properties.getDriverName());
        dsc.setUsername(properties.getUserName());
        dsc.setPassword(properties.getPassword());
        mpg.setDataSource(dsc);
        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(properties.getParentPackageName());
        pc.setEntity("bean.po");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        String templatePath = "/mybatisplustemplate/mapper.xml.ftl";
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return projectPath + "/src/main/resources/mapper/"
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });


        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setEntity("mybatisplustemplate/entity.java");
        templateConfig.setService("mybatisplustemplate/service.java");
        templateConfig.setServiceImpl("mybatisplustemplate/serviceImpl.java");
        templateConfig.setMapper("mybatisplustemplate/mapper.java");
        templateConfig.setController(null);
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass("com.cman777.springc.common.bean.entity.BaseEntity");
        strategy.setSuperServiceClass("com.cman777.springc.mybatis.service.BaseService");
        strategy.setSuperServiceImplClass("com.cman777.springc.mybatis.service.impl.BaseServiceImpl");
        strategy.setSuperMapperClass("com.cman777.springc.mybatis.mapper.CustomBaseMapper");
        strategy.setEntityLombokModel(true);
        strategy.setEntitySerialVersionUID(false);
        strategy.setEntityTableFieldAnnotationEnable(true);
        strategy.setSuperEntityColumns("created_date","state","state_date","operator_id");
        strategy.setInclude(properties.getTableNames().toArray(new String[properties.getTableNames().size()]));
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
    
}
